const app = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    user:{
      avatarUrl:'/images/userPic.png',
      nickName:'微信用户',
      userType:'正式会员',
    },
    userService:[
      {
        jumpPath:'/pages/order/order',
        imgUrl:'/images/order.png',
        title:'我的订单'
      },
      {
        jumpPath:'/pages/collection/collection',      
        imgUrl:'/images/collection.png',
        title:'我的收藏'
      },
      { 
        jumpPath:'/pages/history/history',
        imgUrl:'/images/history.png',
        title:'历史足迹'
      },
      {
        jumpPath:'/pages/comment/comment',
        imgUrl:'/images/comment.png',
        title:'我的评价'
      },
      {
        jumpPath:'/pages/wallet/wallet',
        imgUrl:'/images/wallet.png',
        title:'我的钱包'
      }

    ],
    moreService:[
      {
        cellname:'wallet',
        imgUrl:'/images/wallet.png',
        title:'我的钱包'
      },
      {
        cellname:'feedback',
        imgUrl:'/images/feedback.png',
        title:'意见反馈'
      },
      {
        cellname:'logout',
        imgUrl:'/images/logout.png',
        title:'退出登录'
      }
    ],
    canIGetStoreInfo:false,
  },

  switchPage(){
    if(this.data.isLogin){
      if(app.golbalData.loginByWechat){
        wx.showToast({
          title: '仅支持账号登录用户修改',
          icon:'none',
          duration:2000
        })
      }else{
        wx.navigateTo({
          url: '/pages/userInfo/userInfo',
        })
      }
    }
    else{
      wx.navigateTo({
        url: '/pages/login/login',
      })
    }
  },

  /**
   * 商家申请
   */
  application(){
    if(this.data.canIGetStoreInfo){
      wx.navigateTo({
        url: '/pages/applicationInfo/applicationInfo',
      })
    }else{
      wx.navigateTo({
        url: '/pages/application/application',
      })
    }
  },
  onClickCell(e){
    var type = e.currentTarget.id
    console.log(type)
    switch(type){
      case 'logout':
        this.logout();
        break;
      case 'feedback':{
        wx.navigateTo({
          url: '/pages/feedback/feedback',
        })
        break;
      }
      case 'wallet':{
        wx.navigateTo({
          url: '/pages/wallet/wallet',
        })
        break;
      }
    }
  },

  logout(){
    app.golbalData.isLogin = false
    this.setData({
      isLogin:app.golbalData.isLogin,
      user:{
        avatarUrl:'/images/userPic.png'
      },
    })
    wx.clearStorageSync()
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {

  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    let userInfo = wx.getStorageSync('userInfo')
    let oldInfo = this.data.user
    let newInfo = {...oldInfo,...userInfo}
    this.setData({
      isLogin:app.golbalData.isLogin,
      user:newInfo
    })

    wx.cloud.callFunction({
      name:'getStoreInfo',
      data:{
        phoneNumber:userInfo.account
      }
    }).then(res=>{
      if(res.result.data.length != 0){
        this.setData({
          canIGetStoreInfo:true
        })
      }
    }).catch(error=>{
      console.log(error)
      console.log('数据获取失败')
    })
  },
})